'use strict';

const Service = require('egg').Service;
const { stateKeys } = require('../core/base_mongo_model');

const {
  FEEDBACK_LIST_CATCE_KEY,
  FEEDBACK_LIST_DUSTBIN_CATCE_KEY,
} = require('../../config/redisKey');

class FeedbackService extends Service {
  /**
   * 获取所有数据
   */
  async add() {
    this.ctx.clearCache(
      PACKAGE_LIST_CATCE_KEY,
      PACKAGE_TYPES_CATCE_KEY
    )
    return await this.ctx.model.Feedback.add(this.ctx.w_body());
  }
  async list(all) {
    const p = {};
    if (!all) {
      p.isShow = true;
    }
    return await this.ctx.model.Feedback.findList(p);
  }
  async count(all) {
    const p = {};
    if (!all) {
      p.isShow = true;
    }
    return await this.ctx.model.Feedback.findCount(p);
  }
  /**
   * 删除数据
   */
  async destroy() {
    this.ctx.clearCache(
      FEEDBACK_LIST_CATCE_KEY,
      FEEDBACK_LIST_DUSTBIN_CATCE_KEY,
    )
    const { Feedback } = this.ctx.model;
    return await Feedback.removeModel(this.ctx.params.id);
  }

  /**
   * 更新数据
   */
  async update() {
    his.ctx.clearCache(
      FEEDBACK_LIST_CATCE_KEY,
    )
    const { Feedback } = this.ctx.model;
    return await Feedback.updateModel(this.ctx.params.id, this.ctx.w_body());
  }

  /**
   * 获取回收站列表
   */
  async dustbinList() {
    return this.ctx.getCache(FEEDBACK_LIST_DUSTBIN_CATCE_KEY, async () => {
      return await this.ctx.model.Feedback.find({ $or: [
        { stateKey: stateKeys.DUSTBIN },
      ] }).exec();
    })
  }
  /**
   * 从回收站还原
   */
  async dustbinRestore() {
    this.ctx.clearCache(
      FEEDBACK_LIST_CATCE_KEY,
      FEEDBACK_LIST_DUSTBIN_CATCE_KEY,
    )
    const { Feedback } = this.ctx.model;
    const { id } = this.ctx.params;
    return await Feedback.dustbinRestore(id);
  }
  /**
   * 从回收站删除数据
   */
  async dustbinDelete() {
    this.ctx.clearCache(
      FEEDBACK_LIST_DUSTBIN_CATCE_KEY,
    )
    const { Feedback } = this.ctx.model;
    return await Feedback.removeModel(this.ctx.params.id, true);
  }
}

module.exports = FeedbackService;
